class Solution(object):
    def countNegatives(self, grid):
        m = len(grid)
        n = len(grid[0])
        t = 0
        i = 0
        j = n - 1
        while j >= 0 and i < m:
            while i < m and grid[i][j] >= 0:
                i += 1
            t += m - i
            j -= 1
        return t


data = Solution()
grid = [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]]
print(data.countNegatives(grid))
